﻿using AdminStyem.Application.Command.ProcessingCommand;
using AdminStyem.Domain.Entity;
using AdminStyem.Domain.Entity.Boss;
using AdminStyem.Repositories;
using AutoMapper;
using MediatR;

namespace AdminStyem.Application.CommandHandler.ProcessingCommandHandler
{
    public class ProcessingFirmAddCommandHandler : IRequestHandler<ProcessingFirmAddCommand, APIResult<object>>
    {
        IBaseRespository<ProcessingFirm> BaseProcessFirm;
        IMapper mapper;

        public ProcessingFirmAddCommandHandler(IMapper mapper, IBaseRespository<ProcessingFirm> BaseProcessFirm)
        {
            this.mapper = mapper;
            this.BaseProcessFirm = BaseProcessFirm;
        }

        public Task<APIResult<object>> Handle(ProcessingFirmAddCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> ret = new APIResult<object>();
            ret.Code = ResultCode.添加成功;
            ret.Message = ResultCode.添加成功.ToString();
            var entity = mapper.Map<ProcessingFirm>(request);
            int i = BaseProcessFirm.add(entity).Result;
            if (i <= 0)
            {
                ret.Code = ResultCode.添加失败;
                ret.Message = ResultCode.添加失败.ToString();
                return Task.FromResult(ret);
            }

            return Task.FromResult(ret);
        }
    }
}
